package com.woniuxy.factory.controller;

import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.entity.Schedule;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    ScheduleService ss;

    //查询
    @ResponseBody
    @RequestMapping("allschedule")
    public ResponseResult selectAllSchedule(Schedule schedule,int page,int limit){
        List<Schedule> ls = ss.selectAllSchedule(schedule,page,limit);
        int count = ss.selectScheduleCounts(schedule);
        ResponseResult rr = new ResponseResult(0,"",count,ls);
        return rr;
    }

    //加入生产
    @ResponseBody
    @RequestMapping("/addschedule")
    public ResponseResult addSchedule(String planid,String wosId){
       ResponseResult rr;
        try {
            ss.insertSchedule(planid,wosId);
            rr = new ResponseResult(0,"加入成功",0,null);
        }catch (BusinessException b){
            rr = new ResponseResult(1,b.getMessage(),0,null);
        } catch (Exception e){
            rr = new ResponseResult(2,"加入失败",0,null);
        }
        return rr;
    }

    //更新前先判断当前订单是否已完成，已完成的订单不能更新
    @ResponseBody
    @RequestMapping("/schedulestate")
    public ResponseResult selectScheduleState(String planid){
        ResponseResult rr;
        try {
            ss.selectScheudleState(planid);
            rr = new ResponseResult(0,"",0,null);
        }catch(BusinessException b){
            rr = new ResponseResult(1,b.getMessage(),0,null);
        }
        return rr;
    }

    //数据回显
    @ResponseBody
    @RequestMapping("/scheduleecho")
    public ResponseResult scheduleEcho(String serialNumber){
        Schedule s = ss.scheduleEcho(serialNumber);
        ResponseResult rr = new ResponseResult(0,"",0,s);
        return rr;
    }

    //修改
    @ResponseBody
    @RequestMapping("/updateschedule")
    public ResponseResult upadateSChedule(Schedule schedule){
       ResponseResult rr;
        try {
            ss.upadateSChedcule(schedule);
            rr = new ResponseResult(0,"修改成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"修改失败",0,null);
        }
        return rr;
    }

    //删除
    @ResponseBody
    @RequestMapping("/deleteschedule")
    public ResponseResult deleteSchedule(String serialNumber){
        ResponseResult rr;
        try {
            ss.deleteSchedule(serialNumber);
            rr = new ResponseResult(0,"删除成功",0,null);
        }catch (Exception e){
            rr = new ResponseResult(1,"删除失败",0,null);
        }
        return rr;
    }
}
